package com.yyjzone;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 这是工期排期，时间生成工具
 *
 * @autor: YangYangjun
 * @date: 2021/12/29 15:56
 */
public class WorkTimeCalcTest {
    //工期开始时间
    private final static String start = "2023-03-01";
    //工作制时长
    private final static int work_h = 8;
    //特殊放假日期
    private static Map<String, String> filter = new HashMap<>();
    //特殊工作日
    private static Map<String, String> work_day = new HashMap<>();


    static {
        filter.put("2023-04-05", "清明节");
        filter.put("2023-04-29", "五一节");
        filter.put("2023-04-30", "五一节");
        filter.put("2023-05-01", "五一节");
        filter.put("2023-05-02", "五一节");
        filter.put("2023-05-03", "五一节");
        //
        work_day.put("2023-04-23", "五一补班");
        work_day.put("2023-05-06", "五一补班");
    }

    //工时 小时制 工时尽量设置为 24的约数
    private static int[] workH = {8,
            4,
            8,
            4,
            8,
            4,
            4,
            4,
            4,
            8,
            4,
            4,
            4,
            8,
            4,
            8,
            4,
            8,
            8,
            8,
            8,
            4,
            4,
            4,
            4,
            4,
            4,
            4,
            24,
            4,
            8,
            4,
            16,
            4,
            16,
            8,
            32,
            4,
            4,
            8,
            8,
            4,
            4,
            24};

    /**
     * 输入开始时间-结束时间
     */
    public static void main(String[] args) throws ParseException {
        Date today = new SimpleDateFormat("yyyy-MM-dd").parse(start);
        int b = 24 / work_h;
        int sum = 0;
        for (int h : workH) {
            sum += h;
            int s = 0;
            s += h;
            Calendar c = Calendar.getInstance();
            c.setTime(today);
            Date start = today;
            for (int i = 0; i < s; i++) {
                c.add(Calendar.HOUR_OF_DAY, b);
                int weekDay = c.get(Calendar.DAY_OF_WEEK);
                String str = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
                if (filter.containsKey(str)) {
                    //System.out.println(c.getTime().toLocaleString() + filter.get(str));
                    i--;
                    continue;
                }
                if (work_day.containsKey(str)) {
                    continue;
                }
                if (weekDay == Calendar.SATURDAY) {
                    //System.out.println(c.getTime().toLocaleString() + "是周六");
                    i--;
                    continue;
                }
                if (weekDay == Calendar.SUNDAY) {
                    //System.out.println(c.getTime().toLocaleString() + "是周日");
                    i--;
                }
            }
            today = c.getTime();
            Date end = today;
            if (today.getHours() == 0) {
                Calendar c2 = Calendar.getInstance();
                c2.setTime(today);
                c2.add(Calendar.DATE, -1);
                end = c2.getTime();
            }
            System.out.println("当前工时：" + h + ",开始时间：" + new SimpleDateFormat("yyyy-MM-dd").format(start) + "，结束时间：" + new SimpleDateFormat("yyyy-MM-dd").format(end));
        }
        System.out.println("总共耗时：" + sum + "小时");
    }
}
